﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq.Dynamic;
using Md = Ooohd.Entity;
using DBUtility;
using System.IO;
namespace Ooohd.BllRule
{
    public class ZS_Image
    {
        public void Add(Md.Album.ZS_Image image)
        {
          

            using (var con = DbHelperSQL.GetConnection(0)) { 
                var tran=con.BeginTransaction();
                var ct = new Md.Album.ZS_ImageDataContext(con);
                ct.Transaction = tran;
                ct.ZS_Image.InsertOnSubmit(image);
                ct.SubmitChanges();

                var ct2 = new Md.Album.ZS_AlbumDataContext(con);
                ct2.Transaction = tran;
                var alb=ct2.ZS_Album.Single(a=>a.ID==image.AlbumID);
                alb.ImgCount++;
                ct2.SubmitChanges();

                tran.Commit();
            }
        }

        public void Update(Md.Album.ZS_Image image)
        {
            using (var ct = new Md.Album.ZS_ImageDataContext(DbHelperSQL.Connection))
            {
                ct.ZS_Image.Attach(image, true);
                ct.SubmitChanges();
            }
        }

        public void Delete(int ID)
        {
            
            using (var con = DbHelperSQL.GetConnection(0))
            {
                var tran = con.BeginTransaction();
                var ct = new Md.Album.ZS_ImageDataContext(con);
                ct.Transaction = tran;
                var img=ct.ZS_Image.Single(a=>a.ID==ID);

                var ct2 = new Md.Album.ZS_AlbumDataContext(con);
                ct2.Transaction = tran;
                var alb = ct2.ZS_Album.Single(a => a.ID == img.AlbumID);
                
                if (alb.ImgCount > 0) {
                    alb.ImgCount--;
                    ct2.SubmitChanges();
                }

                ct.ZS_Image.DeleteOnSubmit(img);
                ct.SubmitChanges();

                tran.Commit();
                
            }
        }

        public void Delete(string Ids)
        {
            using (var ct = new Md.Album.ZS_ImageDataContext(DbHelperSQL.Connection))
            {
                string[] idarray = Ids.Split(',');
                var list = ct.ZS_Image.Where(a => idarray.Contains(a.ID.ToString()));
                ct.ZS_Image.DeleteAllOnSubmit(list);
                ct.SubmitChanges();
            }
        }

        public Md.Album.ZS_Image GetSingle(int ID)
        {
            using (var ct = new Md.Album.ZS_ImageDataContext(DbHelperSQL.Connection))
            {
                return ct.ZS_Image.Single(a => a.ID == ID);
            }
        }

        public List<T> Query<T>(string sql, params object[] parameterValues)
        {
            using (var ct = new Md.Album.ZS_ImageDataContext(DbHelperSQL.Connection))
            {
                return ct.ExecuteQuery<T>(sql, parameterValues).ToList();
            }
        }

        public List<Md.Album.ZS_Image> Query(string condition, string orderby, int startIndex, int pageSize, ref int pageCount, params object[] param)
        {
            using (var ct = new Md.Album.ZS_ImageDataContext(DbHelperSQL.Connection))
            {
                IQueryable<Md.Album.ZS_Image> ImageList = ct.ZS_Image;
                if (!string.IsNullOrEmpty(condition))
                    ImageList = ImageList.Where(condition, param);
                if (!string.IsNullOrEmpty(orderby))
                    ImageList = ImageList.OrderBy(orderby);
                pageCount = ImageList.Count();
                return ImageList.Skip(startIndex).Take(pageSize).ToList();
            }
        }

        public List<Md.Album.ZS_Image> Query(string condition, string orderby, params object[] param)
        {
            using (var ct = new Md.Album.ZS_ImageDataContext(DbHelperSQL.Connection))
            {
                IQueryable<Md.Album.ZS_Image> ImageList = ct.ZS_Image;
                if (!string.IsNullOrEmpty(condition))
                    ImageList = ImageList.Where(condition, param);
                if (!string.IsNullOrEmpty(orderby))
                    ImageList = ImageList.OrderBy(orderby);
                return ImageList.ToList();
            }
        }

        public Md.Album.ZS_Image CreateModel()
        {
            return new Md.Album.ZS_Image();
        }
    }
}
